package com.sjf.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjf.common.Result;
import com.sjf.reggie.dto.DishDto;
import com.sjf.reggie.entity.Dish;
import com.sjf.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品控制器
 *
 * @author <huangtang>
 * @since 2022/4/26 13:17
 **/
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {


    @Autowired
    DishService dishService;

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping()
    public Result<String> save( @RequestBody DishDto dishDto ){
        log.info ("菜品信息：{}",dishDto);
        dishService.saveWithFlavor(dishDto);
        return Result.success ("新增菜品成功");
    }

    /**
     * 根据id删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping("")
    public Result<String> deleteByIds(String ids){
        log.info ("删除菜品:{}",ids);
        boolean result = dishService.deleteByIds(ids);
        return result?Result.success ("删除菜品成功"):Result.error ("删除菜品失败");
    }

    /**
     * 修改菜品状态
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> editStatus( @PathVariable("status")Integer status, String ids){
        log.info ("修改状态:{}",status,ids);
        boolean result = dishService.updateStatus (status,ids);
        return result?Result.success ("修改菜品状态成功"):Result.error ("修改菜品状态失败");
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping()
    public Result<String> edit( @RequestBody DishDto dishDto ){
        log.info ("菜品信息：{}",dishDto);
        dishService.edit(dishDto);
        return Result.success ("新增菜品成功");
    }

    /**
     * 套餐页面添加菜品 获取菜品信息
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result<List<DishDto>> getFoodWithSetmeal( Long categoryId,String name,Integer status){
        log.info ("套餐页面添加菜品 获取菜品信息:{},{}",categoryId,name);
        List<DishDto> list = dishService.getFoodWithSetmeal(categoryId,name,status);
        return Result.success (list);
    }

    /**
     * 根据id获取菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishDto> getById(@PathVariable("id") Long id){
        DishDto dishDto = dishService.selectById(id);
        return Result.success (dishDto);
    }

    /**
     * 菜品管理分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page> getList(@RequestParam(value = "page",defaultValue = "1")Integer pageNum,
                                      @RequestParam(value = "pageSize")Integer pageSize,
                                      String name){
        Page dishDtoPage =  dishService.getList(pageNum,pageSize,name);
        return Result.success (dishDtoPage);
    }

}
